<?php
class Category_model extends CI_Model
{
	private $tb_category = 'category';

	public function __contruct()
	{
		parent::__construct();	
	}

	public function listall()
	{
		$query = $this->db->get($this->tb_category);
		return $query->result();
	}

	public function getAllNames()
	{
		$categories = $this->input->post('search');

		$this->db->select('name');
		$this->db->from($this->tb_category);
		$this->db->like('name', $categories);
		$query = $this->db->get();

		$categories_array = array();
		foreach ($query->result() as $row) {
			$categories_array[] = $row->name;
		}

		return $categories_array;
		// $query = $this->db->get($this->tb_category);
		// return $query->result_array();
	}	

	public function AddCategory()
	{
		$data = array(
			'id' => $this->input->post('categoryid'),
			'name' => $this->input->post('categoryname')
			);

		return $this->db->insert('category', $data);
	}

	public function GetById($id)
	{
		$this->db->where('id', $id);
		// $query = $this->db->get($this->tb_category);
		//return $query->result();
		return $this->db->get($this->tb_category);
	}

	// update category by id
	function Update($id, $category)
	{
		$this->db->where('id', $id);
		$this->db->update($this->tb_category, $category);
	}

	function delete($id)
	{
		$this->db->where('id', $id);
		$this->db->delete($this->tb_category);
	}
}
?>